package cn.iocoder.yudao.module.system.controller.admin.partnercoupon;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.partnercoupon.vo.PartnerCouponPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.partnercoupon.vo.PartnerCouponRespVO;
import cn.iocoder.yudao.module.system.controller.admin.partnercoupon.vo.PartnerCouponSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.partnercoupon.PartnerCouponDO;
import cn.iocoder.yudao.module.system.service.partnercoupon.PartnerCouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 合伙人优惠券")
@RestController
@RequestMapping("/system/partnercoupon")
@Validated
public class PartnerCouponController {

    @Resource
    private PartnerCouponService partnerCouponService;

    @PostMapping("/create")
    @Operation(summary = "创建合伙人优惠券")
    @PreAuthorize("@ss.hasPermission('system:partnercoupon:create')")
    public CommonResult<Long> createPartnerCoupon(@Valid @RequestBody PartnerCouponSaveReqVO createReqVO) {
        return success(partnerCouponService.createPartnerCoupon(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合伙人优惠券")
    @PreAuthorize("@ss.hasPermission('system:partnercoupon:update')")
    public CommonResult<Boolean> updatePartnerCoupon(@Valid @RequestBody PartnerCouponSaveReqVO updateReqVO) {
        partnerCouponService.updatePartnerCoupon(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合伙人优惠券")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:partnercoupon:delete')")
    public CommonResult<Boolean> deletePartnerCoupon(@RequestParam("id") Long id) {
        partnerCouponService.deletePartnerCoupon(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合伙人优惠券")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:partnercoupon:query')")
    public CommonResult<PartnerCouponRespVO> getPartnerCoupon(@RequestParam("id") Long id) {
        PartnerCouponDO partnerCoupon = partnerCouponService.getPartnerCoupon(id);
        return success(BeanUtils.toBean(partnerCoupon, PartnerCouponRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合伙人优惠券分页")
    @PreAuthorize("@ss.hasPermission('system:partnercoupon:query')")
    public CommonResult<PageResult<PartnerCouponRespVO>> getPartnerCouponPage(@Valid PartnerCouponPageReqVO pageReqVO) {
        PageResult<PartnerCouponDO> pageResult = partnerCouponService.getPartnerCouponPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PartnerCouponRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合伙人优惠券 Excel")
    @PreAuthorize("@ss.hasPermission('system:partnercoupon:export')")
    @OperateLog(type = EXPORT)
    public void exportPartnerCouponExcel(@Valid PartnerCouponPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PartnerCouponDO> list = partnerCouponService.getPartnerCouponPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合伙人优惠券.xls", "数据", PartnerCouponRespVO.class,
                BeanUtils.toBean(list, PartnerCouponRespVO.class));
    }

}